package com.ycxy.sh.transaction.service.impl;

import com.ycxy.sh.transaction.dao.entity.News;
import com.ycxy.sh.transaction.dao.vo.LayuiPageVo;
import com.ycxy.sh.transaction.ex.ServiceException;
import com.ycxy.sh.transaction.mapper.NewsMapper;
import com.ycxy.sh.transaction.service.NewsService;
import com.ycxy.sh.transaction.util.KeyUtil;
import com.ycxy.sh.transaction.web.JsonResult;
import com.ycxy.sh.transaction.web.ServiceCode;
import lombok.extern.slf4j.Slf4j;
import org.apache.ibatis.annotations.Param;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.ui.ModelMap;
import org.springframework.util.StringUtils;

import javax.servlet.http.HttpSession;
import java.util.List;

/**
 * 公告管理实现类
 * @author hlt
 * @since 2019-12-21
 */
@Slf4j
@Service
@Transactional
public class NewsServiceImpl implements NewsService {
    @Autowired
    private NewsMapper newsMapper;

    /**
     * 发布公告
     * @param news
     * @param session
     */
    public void insertNews(News news, HttpSession session){
        log.debug("开始处理发布公告业务...");
        String username=(String) session.getAttribute("username");
        news.setNewsId(Long.valueOf(KeyUtil.genUniqueKey())).setUsername(username);
        int row = newsMapper.insert(news);
        if (row != 1){
            String message="公告发布失败";
            log.debug(message);
            throw new ServiceException(ServiceCode.ERR_INSERT,message);

        }
    }

    /**删除公告*/
    public void delectNews(Long newsId,HttpSession session){
        log.debug("开始处理删除公告业务...");
        String username = (String) session.getAttribute("username");
        News news = newsMapper.selectNewsById(newsId);
        if (StringUtils.isEmpty(news)){
            String message="找不到要删除的公告";
            log.debug(message);
            throw new ServiceException(ServiceCode.ERR_INSERT,message);
        }else {
            /**判断是否是本人或超级管理员*/
            if (news.getUsername().equals(username) || username.equals("admin")){
                int row = newsMapper.delectById(newsId);
                if (row != 1){
                    String message="删除公告失败";
                    log.debug(message);
                    throw new ServiceException(ServiceCode.ERR_DELETE,message);
                }
            }else {
                throw new ServiceException(ServiceCode.ERR_FORBIDDEN,"您的权限不足，无法删除");
            }
        }
    }

    /**
     * 根据传入的公告id修改公告内容
     * @param news
     */
    public void updateNews(News news){
        int i = newsMapper.updateById(news);
        if (i != 1){
            String message="修改公告失败,请稍后重试";
            log.debug(message);
            throw new ServiceException(ServiceCode.ERR_UPDATE,message);
        }
    }

    /**查看公告详情*/
    public News queryNewsById(Long newsid){
        //浏览量+1
        int row = newsMapper.addNewsPageview(newsid);
        if(row!=1){
            String message="操作失败,请稍后重试";
            log.debug(message);
            throw new ServiceException(ServiceCode.ERR_SELECT,message);
        }
        News news = newsMapper.selectNewsById(newsid);
        return news;
    }

    /**查询前三条公告*/
    public List<News> queryNews(){
        List<News> newslist = newsMapper.queryNews();
        if(newslist==null){
            String message="查询失败,请稍后重试";
            log.debug(message);
            throw new ServiceException(ServiceCode.ERR_SELECT,message);
         }
        return newslist;
    }

    /**
     * 后台分页查看公告列表
     * @param limit
     * @param page
     * @return
     */
    @Override
    public LayuiPageVo selectListNews(int limit, int page) {
        List<News> newsList = newsMapper.pageNews((page - 1) * limit, limit);
        int dataNumber = newsMapper.Count();
        if(dataNumber<1){
            String message="查询失败,公告信息不存在";
            log.debug(message);
            throw new ServiceException(ServiceCode.ERR_SELECT,message);
        }
        return new LayuiPageVo("",0,dataNumber,newsList);
    }

    /**
     * 分页首页网站公告
     * @param page
     */
    @Override
    public List<News> selectHomePage(Integer page) {
        List<News> newsList = newsMapper.pageNews((page - 1) * 9, 9);
        if(newsList==null){
            String message="查询失败,公告信息不存在";
            log.debug(message);
            throw new ServiceException(ServiceCode.ERR_SELECT,message);
        }
        log.debug("分页查询首页公告成功");
        return newsList;
    }


}
